﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Collections.Generic;

namespace DreamExplanation
{
    public class Utility
    {
        public static void GoMainPageWithBack(NavigationService NavigationService, string MainpageURL)
        {
            while (NavigationService.CanGoBack)
            {
                IEnumerator<JournalEntry> list = NavigationService.BackStack.GetEnumerator();
                list.MoveNext();
                JournalEntry current = list.Current;
                string uri = current.Source.ToString();
                if (uri == MainpageURL)
                {
                    NavigationService.GoBack();
                }
                else
                {
                    NavigationService.RemoveBackEntry();
                }
            }
        }

        public static void BoldText(ref TextBlock tb, string partToBold, Color color)
        {

            string Text = tb.Text;
            string tempText = Text;
            int startIndex = 0;
            List<int> indexList = new List<int>();
            int index=0;
            while ((index = tempText.IndexOf(partToBold, startIndex)) >= 0)
            {
                indexList.Add(index);
                startIndex = index + partToBold.Length;
                if (startIndex > tempText.Length - 1)
                {
                    break;
                }
            }
            tb.Inlines.Clear();
            for (int n = 0; n < indexList.Count;n++ )
            {
                Run r = new Run();
                if (n == 0)
                {
                    r.Text = Text.Substring(0, indexList[n]);
                }
                else
                {
                    r.Text = Text.Substring(indexList[n - 1] + partToBold.Length, indexList[n] - indexList[n - 1] - partToBold.Length);
                }
                tb.Inlines.Add(r);
                r = new Run();
                r.Text = partToBold;
                r.FontWeight = FontWeights.Bold;
                r.Foreground = new SolidColorBrush(color);
                tb.Inlines.Add(r);
                if (n + 1 >= indexList.Count)
                {
                    r = new Run();
                    r.Text = Text.Substring(indexList[n] + partToBold.Length, Text.Length - (indexList[n] + partToBold.Length));
                    tb.Inlines.Add(r);
                }
                //else
                //{
                //    r = new Run();
                //    r.Text = Text.Substring(indexList[n] + partToBold.Length, indexList[n + 1] - indexList[n] - partToBold.Length);
                //    tb.Inlines.Add(r);
                //}
                
            }
            
            
        }

        
    }
}
